window.addEventListener('load', function () {
    // this.alert('1')
    // 获取变量
    var focus = document.querySelector('.focus')
    var ul = focus.children[0]
    var w = focus.offsetWidth
    var ol = focus.children[1];
    var index = 0;

    var timer = setInterval(function () {

        index++;

        var translatex = -index * w;
        ul.style.transition = 'all .3s'
        ul.style.transform = 'translateX(' + translatex + 'px)';

    }, 2000)
    ul.addEventListener('transitionend', function () {
        if (index == ul.children.length - 2) {
// 当最后一张图片的过度走完，实际上他已经走到了那个位置
            index = 0;
            ul.style.transition = 'none';
            var translatex = -index * w;
            ul.style.transform = 'translateX(' + translatex + 'px)';
        }
        else if (index < 0) {
            index = ul.children.length - 2;
            ul.style.transition = 'none';
            var translatex = -index * w;
            ul.style.transform = 'translateX(' + translatex + 'px)';
        }
        ol.querySelector('.current').classList.remove('current')
        ol.children[index].classList.add('current')
    })
    // 手指滑动轮播图的时候
    var startX = 0;
    var moveX = 0;
    // 如果移动过，再计算手松开后的事
    var flag = false
    ul.addEventListener('touchstart', function (e) {
        // 获得手指刚触碰时候的坐标
        startX = e.targetTouches[0].pageX;
        clearInterval(timer)
    })
    ul.addEventListener('touchmove', function (e) {
        moveX = e.targetTouches[0].pageX - startX;
        var translatex = -index * w + moveX;
        ul.style.transition = 'none'
        ul.style.transform = 'translateX(' + translatex + 'px)';
        flag = true;
        e.preventDefault();
    })
    ul.addEventListener('touchend', function (e) {
        if(flag){

        
       if(Math.abs(moveX) > 50){
         if(moveX>0){
             index--
         }else{
             index++
         }
         var translatex = -index * w;
         ul.style.transition = 'all .3s'
            ul.style.transform = 'translateX(' + translatex + 'px)';
       }
       else {
        var translatex = -index * w;
        ul.style.transition = 'all .3s'
        ul.style.transform = 'translateX(' + translatex + 'px)';
       }
       clearInterval(timer)
        timer = setInterval(function () {

        index++;

        var translatex = -index * w;
        ul.style.transition = 'all .3s'
        ul.style.transform = 'translateX(' + translatex + 'px)';

    }, 2000)
}

    })
    var goback = document.querySelector('.goBack')
    var nav = document.querySelector('.local-nav')

    window.addEventListener('scroll',function(e){
        // 卷走的距离
        console.log(nav.offsetTop);
        
        if(window.pageYOffset>=nav.offsetTop){
            goback.style.display = 'block'
        }else{
            goback.style.display = 'none'
        }
           
    })
    goback.addEventListener('click',function(){
        // document.style.transition = 'all .3s'
        // document.style.top = 0
        // // window.pageYOffset
        window.scroll(0,0)
    })

})